package com.example.ribbonconsumer.controller;

import com.example.ribbonconsumer.entity.User;
import com.example.ribbonconsumer.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import javax.jws.soap.SOAPBinding;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zcx
 * @Title 订单控制器
 * @date 2019年03月31日 12:30
 **/
@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/getOrderUser")
    public String getOrderUser() {
        return restTemplate.getForEntity("http://eureka-client/getUser",String.class).getBody();
    }

    @RequestMapping(value = "/getOrderUserName/{id}")
    public String getUserName(@PathVariable("id") String id) {
        return orderService.getUserName(id);
    }

    @RequestMapping(value = "getOrderUserInfo/{id}")
    public User getOrderUserInfo(@PathVariable("id") String id) {
        return restTemplate.getForEntity("http://eureka-client/userInfo?id={1}",User .class,id).getBody();
    }

    @RequestMapping(value = "getOrderInfo/{name}")
    public User getOrderInfo(@PathVariable("name") String name) {
        Map<String,String> map = new HashMap<>();
        map.put("name",name);
        return restTemplate.getForEntity("http://eureka-client/Info?name={name}",User .class,map).getBody();
    }

    @RequestMapping(value = "getOrderUri/{name}")
    public User getOrderUri(@PathVariable("name") String name) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(
                "http://eureka-client/Info?name={name}")
                .build()
                .expand(name)
                .encode();
        URI uri = uriComponents.toUri();
        return restTemplate.getForEntity(uri,User.class).getBody();
    }

    @RequestMapping(value = "/postOrderUserName/{id}")
    public User postUserName(@PathVariable("id") String id) {
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("id",id);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForEntity("http://eureka-client/postNsername",entity,User.class).getBody();
    }

    @RequestMapping("/refresh")
    public void refresh(){
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置content_type为json要不然会报415的错误
        httpHeaders.add(HttpHeaders.CONTENT_TYPE,"application/json");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(null,httpHeaders);
        //以post方法访问真正的刷新链接
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://config-server/actuator/refresh",
                request, String.class);
//        restTemplate.getForEntity("http://config-server/monitor",String.class).getBody();
    }
}
